/*
 * @Author: wusd
 * @Date: 2022-06-15 10:08:43
 * @LastEditors: wusd
 * @LastEditTime: 2022-06-15 11:37:36
 * @Description:
 *
 */
class Storage {
  privatePreKey: string
  // 所有均添加上项目私有前缀
  constructor(name?: string | undefined) {
    this.privatePreKey = name ? name + '_' : process.env.VUE_APP_NAME ? process.env.VUE_APP_NAME + '_' : ''
  }
  get(key: string) {
    if (!key) return
    let value = window.localStorage.getItem(this.privatePreKey + key) || ''
    try {
      value = JSON.parse(value)
      return value
    } catch (e) {
      return value
    }
  }

  set(key: string, value: string) {
    if (!key) return
    if (['[object Object]', '[object Array]'].includes(Object.prototype.toString.call(value))) {
      value = JSON.stringify(value)
    }
    window.localStorage.setItem(this.privatePreKey + key, value)
  }

  remove(key: string) {
    if (!key) return
    window.localStorage.removeItem(this.privatePreKey + key)
  }
}

export default new Storage()
